package com.yuki.controller;

import com.yuki.entity.User;
import com.yuki.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/query/{id}")
    public User findUserById(@PathVariable("id") Integer id) {
        User user = userService.findUser(id);
        return user;
    }

    @RequestMapping(value = "/delete/{id}")
    public String deleteUserById(@PathVariable("id") Integer id) {
        int res = userService.deleteUser(id);
        return res == 1 ? "success" : "failed";
    }

    @RequestMapping(value = "/save/{id}/{userName}/{passWord}")
    public String saveUser(@PathVariable("id") Integer id, @PathVariable("userName") String userName, @PathVariable("passWord") String passWord) {
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setPassWord(passWord);
        int res = userService.saveUser(user);
        return res == 1 ? "success" : "failed";
    }
}
